package com.puman.watchtrade.fragment.info.adpter;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.fragment.home.imageLoader.util.CheckImageLoaderConfiguration;
import com.puman.watchtrade.fragment.home.imageLoader.util.UniversalImageLoadTool;
import com.puman.watchtrade.util.Gjfun;
import com.puman.watchtrade.util.GlobalData;
import com.puman.watchtrade.util.ImageDownloader;
import com.puman.watchtrade.util.OnImageDownload;

public class ImgAdapter extends BaseAdapter {

	private Context _context;
	private List<String> imgList;
//	private List<String> titleList;
	private ImageDownloader mDownloader;
	private Activity activity;
	private LayoutInflater listContainer; // ��ͼ����

	public final class ViewHolder {
		ImageView imageView;
		TextView infoDetail;
	}

	public ImgAdapter(Context context, List<String> imgList, Activity activity) {
		_context = context;
		this.imgList = imgList;
		this.activity = activity;
		listContainer = LayoutInflater.from(activity);
	}

	public int getCount() {
		return Integer.MAX_VALUE;
	}

	public Object getItem(int position) {

		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = listContainer.inflate(R.layout.img_item, null);
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.info_item_image);
//			viewHolder.infoDetail = (TextView) convertView
//					.findViewById(R.id.info_detail);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		int height = (int) (GlobalData.screenWidth *((float)450/(float)640));
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,height);
		viewHolder.imageView.setLayoutParams(layoutParams);
//		if (GlobalData.userInfo.headPicture.trim() != Config.baseUrlStr+Config.imgUrl) {
//			// 如果有头像，载入，否则显示layout上面的（@drawable/home_new_icon）
//			Log.d("HeadPicture", GlobalData.userInfo.headPicture.trim());
//		if (imgList.size() != 0) {
			CheckImageLoaderConfiguration.checkImageLoaderConfiguration(_context);
			String url = imgList.get(position% imgList.size()).replace(" ", "");
			UniversalImageLoadTool.display(url, (ImageView)viewHolder.imageView, R.drawable.icon);
//String urlString= imgList.get(position% imgList.size());
//			viewHolder.imageView.setImageURI(uri);
			//		}
			
//		}
//		if (!imgList.get(position % imgList.size()).equals("")) {
//
//			String url = "";
//			url = imgList.get(position % imgList.size());
//			viewHolder.imageView.setTag(imgList.get(position % imgList.size()));
//			if (mDownloader == null) {
//				mDownloader = new ImageDownloader();
//			}
//			if (mDownloader != null) {
//				mDownloader.imageDownload(url, viewHolder.imageView,
//						"/watchtrade/image", activity, new OnImageDownload() {
//							@Override
//							public void onDownloadSucc(Bitmap bitmap,
//									String c_url, ImageView mimageView) {
//								ImageView imageView = (ImageView) viewHolder.imageView
//										.findViewWithTag(c_url);
//								Drawable drawable = new BitmapDrawable(bitmap);
//								if (imageView != null) {
//									imageView.setBackgroundDrawable(drawable);
//									imageView.setTag("");
//								}
//							}
//						});
//			}
//		}

		return convertView;
	}

}
